home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / threads / ThreadsUIOTTY.c < prev    next >
C/C++ Source or Header  |  1991-06-24  |  3KB  |  116 lines

  1. /* begincopyright
  2.   Copyright (c) 1988-1990 Xerox Corporation. All rights reserved.
  3.   Use and copying of this software and preparation of derivative works based
  4.   upon this software are permitted. Any distribution of this software or
  5.   derivative works must comply with all applicable United States export
  6.   control laws. This software is made available AS IS, and Xerox Corporation
  7.   makes no warranty about the software, its performance or its conformity to
  8.   any specification. Any person obtaining a copy of this software is requested
  9.   to send their name and post office or electronic mail address to:
  10.     PCR Coordinator
  11.     Xerox PARC
  12.     3333 Coyote Hill Rd.
  13.     Palo Alto, CA 94304
  14.   endcopyright */
  15.  
  16. /*
  17.  * ThreadsUIOTTY.c
  18.  *
  19.  * Demers, July 16, 1990 3:21:58 pm PDT
  20.  */
  21.  
  22. #include "xr/UIO.h"
  23. #include "xr/UIOPrivate.h"
  24. #include "xr/Errno.h"
  25. #include "xr/GCVirtualDirty.h"
  26.  
  27. #include <sys/fcntl.h>
  28. #include <sys/termios.h>
  29.  
  30.  
  31. XR_Fildes
  32. XR_TTYOpen(name, flags, cflag)
  33.     char *name;
  34.     unsigned flags;
  35.     unsigned cflag;
  36. {
  37.     XR_Fildes fd = XR_nullFildes;
  38.     int ans;
  39.     unsigned iflag = 0;
  40.     unsigned oflag = 0;
  41.     struct termios cb;
  42.     
  43.     fd = XR_Open(name, flags, 0666);
  44.     if( fd == XR_nullFildes ) goto Bad;
  45.     ans = XR_IOCtl4(fd, TCGETS, &cb, TRUE);
  46.     if( ans < 0 ) goto Bad;
  47.     if( cflag != 0 ) cb.c_cflag = cflag;
  48.     cb.c_iflag = iflag;
  49.     cb.c_oflag = oflag;
  50.     cb.c_lflag = 0;
  51.     cb.c_cc[VMIN] = 0;
  52.     cb.c_cc[VTIME] = 0;
  53.     ans = XR_IOCtl4(fd, TCSETS, &cb, TRUE);
  54.     if( ans < 0 ) goto Bad;
  55.     return fd;
  56.   Bad:
  57.     if( fd != XR_nullFildes ) {
  58.         int sav = XR_GetErrno();
  59.         (void) XR_Close(fd);
  60.         XR_SetErrno(sav);
  61.     }
  62.     return (-1);
  63. }
  64.  
  65.  
  66.  
  67. static XR_FDE_FDC_WORKER(XR_TTYReadWorker)
  68. {
  69.     int ans;
  70.     XR_ProtectSysCall(x1,x2);
  71.     ans = read(fdc->fdc_index, x1, x2);
  72.     XR_UnprotectSysCall();
  73.     if( ans == 0 ) {
  74.         XR_SetErrno(EWOULDBLOCK);
  75.         return (-1);
  76.     }
  77.     return(ans);
  78. }
  79.  
  80.  
  81.  
  82. int
  83. XR_TTYRead(fildes, buf, nBytes)
  84.     XR_Fildes fildes;
  85.     XR_Pointer buf;
  86.     unsigned nBytes;
  87. {
  88.     int n, nRead;
  89.     XR_UIOWaitReadyProc waitReadyProc;
  90.  
  91.     nRead = 0;
  92.     for(
  93.         waitReadyProc = &XR_UIOIn0WaitReadyProc;
  94.         ;
  95.         waitReadyProc = &XR_UIOIn1WaitReadyProc
  96.     ) {
  97.     n = XR_UIODoWithFDEAndFDC(
  98.         fildes,
  99.         waitReadyProc,
  100.         XR_TTYReadWorker,
  101.         ((unsigned)(buf+nRead)),
  102.         ((unsigned)(nBytes-nRead))
  103.     );
  104.     if( (n <= 0) || ((nRead += n) >= nBytes) ) break;
  105.     }
  106.     if( n >= 0 ) return(nRead);
  107.     switch( XR_GetErrno() ) {
  108.     case EWOULDBLOCK:
  109.     case EAGAIN:    /* System V version of EWOULDBLOCK */
  110.         if( nRead > 0 ) return(nRead);
  111.         break;
  112.     }
  113.     return(-1);
  114. }
  115.  
  116.